package com.lc.cehui.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.http.EasyHttp;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppFragment;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.GoodsIndexApi;
import com.lc.cehui.http.request.GoodsListApi;
import com.lc.cehui.http.response.BaseBean;
import com.lc.cehui.http.response.GoodIndexBean;
import com.lc.cehui.http.response.GoodsListBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.GoodsDetailsActivity;
import com.lc.cehui.ui.activity.MainActivity;
import com.lc.cehui.ui.activity.home.SoftwareSalesActivity;
import com.lc.cehui.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

public class ShopFragment extends AppFragment<MainActivity> {

    @BindView(R.id.recyclerView_type)
    RecyclerView recyclerViewType;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView_shop)
    RecyclerView recyclerViewShop;
    List<GoodIndexBean.DataBean> listType = new ArrayList<>();
    List<GoodsListBean.DataBean> listShop = new ArrayList<>();
    private BaseQuickAdapter adapterType;
    private BaseQuickAdapter adapterShop;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    protected void initView() {
        recyclerViewType.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerViewShop.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerViewType.setAdapter(adapterType = new BaseQuickAdapter<GoodIndexBean.DataBean, BaseViewHolder>(R.layout.adapter_shop_type) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, GoodIndexBean.DataBean data) {
                baseViewHolder.setText(R.id.tv_title, data.getTitle());
                if (data.isSelect()) {
                    baseViewHolder.setTextColor(R.id.tv_title, getResources().getColor(R.color.blue028));
                    baseViewHolder.setBackgroundColor(R.id.view_item, getResources().getColor(R.color.blue028));
                } else {
                    baseViewHolder.setTextColor(R.id.tv_title, getResources().getColor(R.color.color333333));
                    baseViewHolder.setBackgroundColor(R.id.view_item, getResources().getColor(R.color.transparent));
                }
            }
        });
        recyclerViewShop.setAdapter(adapterShop = new BaseQuickAdapter<GoodsListBean.DataBean, BaseViewHolder>(R.layout.adapter_shop_item) {

            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, GoodsListBean.DataBean dataBean) {
                baseViewHolder.setText(R.id.tv_name,dataBean.getName()).setText(R.id.tv_money,"￥"+dataBean.getPrice());
                GlideApp.with(getContext()).load(dataBean.getCover_img()).into((ShapeableImageView) baseViewHolder.getView(R.id.iv_logo));
            }
        });
        adapterType.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                for (int i = 0; i < listType.size(); i++) {
                    if (listType.get(position).getTitle().equals(listType.get(i).getTitle())) {
                        listType.get(i).setSelect(true);
                        tvTitle.setText(listType.get(i).getTitle());
                    } else {
                        listType.get(i).setSelect(false);
                    }
                }
                adapterType.notifyDataSetChanged();
                initData(listType.get(position).getId());
            }
        });

        adapterShop.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startActivity(new Intent(getContext(),GoodsDetailsActivity.class).putExtra("id",listShop.get(position).getId()));
            }
        });
    }

    @Override
    protected void initData() {
        EasyHttp.post(this)
                .api(new GoodsIndexApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                        .setToken(SPUtil.get(getContext(), IntentKey.TOKEN, "")))
                .request(new HttpCallback<GoodIndexBean>(this) {
                    @Override
                    public void onSucceed(GoodIndexBean data) {
                        LogUtils.e(JSON.toJSONString(data));
                        if (callBackCode(data)){
                            if (data.getData().size()!=0){
                                listType.addAll(data.getData());
                                adapterType.setNewInstance(listType);
                                adapterType.notifyDataSetChanged();
                                data.getData().get(0).setSelect(true);
                                if (data.getData().get(0).getGoods().size()!=0){
                                    initData(data.getData().get(0).getId());
                                }
                            } else {
                                showEmptyView(adapterShop);
                            }
                        }
                    }
                });
    }

    private void initData(String id){
        showLoadView(adapterShop);
        EasyHttp.post(this)
                .api(new GoodsListApi().setId(id).setPage(1)
                .setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                .setToken(SPUtil.get(getContext(), IntentKey.TOKEN, "")))
                .request(new HttpCallback<GoodsListBean>(this) {
                    @Override
                    public void onSucceed(GoodsListBean data) {
                        LogUtils.e(JSON.toJSONString(data));
                        if (callBackCode(data)){
                            if (data.getData().size()!=0){
                                listShop.clear();
                                listShop.addAll(data.getData());
                                adapterShop.setNewInstance(listShop);
                            } else {
                                showEmptyView(adapterShop);
                            }
                            adapterShop.notifyDataSetChanged();
                        }
                    }
                });
    }


}
